package com.briup.cloud.framework.resource.config;

import com.briup.framework.utils.web.Response;
import com.briup.framework.utils.web.ResponseCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

/**
 * @Author guomiao
 * @Date 2022/8/11 19:49
 */

public class Auth2ResponseExceptionTranslator implements WebResponseExceptionTranslator {
    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
        Throwable throwable = e.getCause();
        if(throwable!=null && (throwable instanceof InvalidTokenException)){
            return new ResponseEntity(Response.error(ResponseCode.INVALID_TOKEN,"请重新登录！"), HttpStatus.OK);
        }else if(e instanceof InsufficientAuthenticationException){
            return new ResponseEntity(Response.error(ResponseCode.USER_NOT_LOGIN,"请先登录！"), HttpStatus.OK);
        }
        return new ResponseEntity(Response.error(ResponseCode.ERROR,e.getMessage()),HttpStatus.OK);
    }
}
